﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;
using BaseSet.Framework.Util;


namespace WebSite.Company
{
    public partial class CompanyAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

            }
        }


        #region 提交
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Users uinfo = UserBase.GetCurrentUser();
            string companyname = txtCompanyName.Text.Trim();
            string brief = txtBrief.Text.Trim();
            string realmname = txtRealmName.Text.Trim();
            //判断该小组个性域名是否已经存在
            Companys cinfo = new Companys();
            CompanysHelper chelper = new CompanysHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "RealmName", realmname);
            int count = chelper.QueryCompanysCountBySearch(c);
            if (count > 0)
            {
                Tools.Alert("个性域名已存在，请输入其他");
                return;
            }
            else
            {

                int type = 1;
                if (rbCheck.Checked)
                {
                    type = 1;
                }
                else if (rbNotCheck.Checked)
                {
                    type = 2;
                }

                //小组头像
                string fileFullName = "";
                string newfilename = "";
                string filePath = "";
                string fileFolder = "/Files/headphoto/";

                string originalImagePath = "";
                string thumbnailPath = "";

                if (fuFile.HasFile)
                {
                    //判断文件的大小
                    string FileName = fuFile.FileName.ToString();
                    string ExtenName = System.IO.Path.GetExtension(FileName);//获取扩展名
                    if (ExtenName.ToLower() == ".jpg" || ExtenName.ToLower() == ".png" || ExtenName.ToLower() == ".bmp" || ExtenName.ToLower() == ".jpeg" || ExtenName.ToLower() == ".gif")
                    {
                        string serverPath = "..\\Files\\headphoto\\";
                        filePath = Server.MapPath(serverPath);
                        string newfilepath = filePath;

                        if (!System.IO.Directory.Exists(filePath))
                        {
                            System.IO.Directory.CreateDirectory(filePath);
                        }

                        string filename = DateTime.Now.ToString("yyyyMMddhhmmss");

                        fileFullName = filename + ExtenName;
                        newfilename = filename + "new" + ExtenName;
                        filePath += fileFullName;
                        newfilepath += newfilename;
                        fuFile.SaveAs(filePath);

                        //压缩图片
                        originalImagePath = filePath;
                        thumbnailPath = newfilepath;

                        int width = 150;
                        int height = 150;
                        string mode = "Cut";
                        string imageType = "";
                        if (ExtenName.ToLower() == ".gif")
                        {
                            imageType = ".gif";
                        }
                        else
                        {
                            imageType = ".jpg";
                        }
                        ImageUtils.MakeThumbnail(originalImagePath, thumbnailPath, width, height, mode, imageType);
                        //将原图片删除
                        System.IO.File.Delete(filePath);
                    }
                    else
                    {
                        Tools.Alert("图片格式不正确");
                        return;
                    }
                }

                cinfo.CompanyName = companyname;
                cinfo.Brief = brief;
                cinfo.RealmName = realmname;
                cinfo.CompanyLevelID = "1";
                cinfo.OwnerUserID = uinfo.ID;
                cinfo.OwnerNickName = uinfo.NickName;
                cinfo.Type = type;
                cinfo.UserNum = 1;
                cinfo.CareNum = 1;
                cinfo.Flag = 0;
                if (!string.IsNullOrEmpty(thumbnailPath))
                {
                    cinfo.SmallHeadImage = fileFolder + newfilename;
                }
                chelper.AddCompanys(cinfo);

                //将创建用户加入到公司用户中
                UserCompany ucinfo = new UserCompany();
                UserCompanyHelper uchelper = new UserCompanyHelper();
                ucinfo.UsersID = uinfo.ID;
                ucinfo.NickName = uinfo.NickName;
                ucinfo.CompanyID = cinfo.ID;
                ucinfo.CompanyOwnerID = uinfo.ID;
                ucinfo.Status = 1;
                uchelper.AddUserCompany(ucinfo);

                //该用户自动关注该小组
                UserCare uccinfo = new UserCare();
                UserCareHelper ucchelper = new UserCareHelper();
                uccinfo.UsersID = uinfo.ID;
                uccinfo.ObjectType = 1;
                uccinfo.ObjectID = cinfo.ID;
                ucchelper.AddUserCare(uccinfo);

                Tools.AlertAndRedirect("创建小组成功", "CompanyList.aspx");
            }
        }
        #endregion

    }
}